package com.fatmeraid.controller.policy;

import com.fatmeraid.common.login.LoginUser;
import com.fatmeraid.common.login.LoginUserHolder;
import com.fatmeraid.common.model.PageResult;
import com.fatmeraid.common.result.Result;
import com.fatmeraid.domain.Policy;
import com.fatmeraid.service.PolicyService;
import com.fatmeraid.vo.PaginationVo;
import com.fatmeraid.vo.PolicyVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "扶贫政策管理")
@RestController
@RequestMapping("/policy")
public class PolicyController {

	@Autowired
	private PolicyService policyService;

	@Operation(summary = "获取政策列表")
	@GetMapping("/list")
	public Result<PageResult<Policy>> getPolicyList(PaginationVo paginationVo) {
		PageResult<Policy> policyList = policyService.getPolicyList(paginationVo);
		return Result.ok(policyList);
	}

	@Operation(summary = "获取管理员的政策列表")
	@GetMapping("/admin/list")
	public Result<PageResult<Policy>> getAdminPolicyList(PaginationVo paginationVo) {
		PageResult<Policy> policyList = policyService.getAdminPolicyList(paginationVo);
		return Result.ok(policyList);
	}

	@Operation(summary = "获取政策详情")
	@GetMapping("/detail/{policyId}")
	public Result<Policy> getPolicyDetail(@PathVariable Long policyId) {
		Policy policy = policyService.getPolicyDetail(policyId);
		return Result.ok(policy);
	}

	@Operation(summary = "添加政策")
	@PostMapping
	public Result<Boolean> addPolicy(@RequestBody Policy policy) {
		LoginUser loginUser = LoginUserHolder.getLoginUser();
		policy.setPublisherId(loginUser.getUserId());
		policyService.addPolicy(policy);
		return Result.ok();
	}

	@Operation(summary = "更新政策")
	@PutMapping
	public Result<Boolean> updatePolicy(@RequestBody Policy policy) {
		boolean success = policyService.updatePolicy(policy);
		return Result.ok();
	}

	@Operation(summary = "删除政策")
	@DeleteMapping("/{policyId}")
	public Result<Boolean> deletePolicy(@PathVariable Long policyId) {
		boolean success = policyService.deletePolicy(policyId);
		return Result.ok();
	}
}